package com.campus.client.controller;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.campus.pojos.domain.dto.PhoneAccountDto;
import com.campus.pojos.domain.dto.UserAccountDto;
import com.campus.pojos.domain.dto.UserDto;
import com.campus.pojos.domain.vo.UserLoginVo;
import com.campus.pojos.domain.vo.UserVo;
import com.campus.result.Result;
import com.campus.service.UserService;
import com.campus.sms.Sample;
import com.campus.utils.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;


@RestController
@RequestMapping("/client/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/code/{phone}")
    public Result<String> code(@PathVariable("phone") String phone) throws Exception {
        //验证手机号是否合法
        if(!SmsUtils.isMobileNumber(phone)){
            return Result.error("手机号不合法！");
        }
        //生成0-1000随机数，少位数前面补0
        int RandomNum = new Random().nextInt(1000);
        String fourDigitNum = String.format("%04d", RandomNum);
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.auth("123456");
        jedis.set("code", fourDigitNum);//设置值
        jedis.expire("code",60000);//设置过期时间

        Client client = SmsUtils.createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("CAMPUSGO")
                .setTemplateCode("SMS_465671094")
                .setTemplateParam("{\"code\":\""+fourDigitNum+"\"}");

        RuntimeOptions runtime = new RuntimeOptions();
        // 复制代码运行请自行打印 API 的返回值
        client.sendSmsWithOptions(sendSmsRequest, runtime);
        return Result.success(fourDigitNum);
    }

    /**
     * 用户登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVo> login(@RequestBody UserAccountDto dto){
        log.info("用户登录：{}", dto);
        return userService.login(dto);
    }

    /**
     * 手机号登录
     * @param dto
     * @return
     */
    @PostMapping("/phoneLogin")
    public Result<UserLoginVo> loginByPhone(@RequestBody UserAccountDto dto){
        log.info("手机号登录：{}", dto);
        return userService.loginByPhone(dto);
    }

    /**
     * 获取用户名
     * @return
     */
    @GetMapping("/getUserId")
    public Result<Long> getUsername() {
        //userService.getId();
        return  Result.success();

    }

    /**
     * 用户注册
     * @param dto
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserAccountDto dto){
        log.info("用户注册：{}", dto);
        return userService.register(dto);
    }

    /**
     * 用户注册
     * @param dto
     * @return
     */
    @PostMapping("/phoneRegister")
    public Result registerByPhone(@RequestBody UserAccountDto dto){
        log.info("用户注册通过手机号 ：{}", dto);
        return userService.registerByPhone(dto);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Result<UserVo> getById(@PathVariable Long id){
        log.info("根据id查询用户信息：{}",id);
        return userService.getByUserId(id);
    }


    /**
     * 编辑用户信息
     * @param dto
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody UserDto dto){
        log.info("修改用户信息 {}", dto);
        return userService.edit(dto);
    }

    /**
     * 获取用户点赞帖子
     */
    @GetMapping("/loveArticle/{id}")
    public Result<List<Long>> getLoveArticle(@PathVariable Long id){
        log.info("查询用户id为{}的点赞帖子id", id);
        return userService.getLove(id);
    }

}
